package com.roads.project.controller;

import java.util.List;

import com.roads.common.core.domain.entity.SysUser;
import com.roads.system.domain.SysRoleReport;
import com.roads.system.domain.SysUserReport;
import com.roads.system.domain.SysUserRole;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.project.domain.ProReport;
import com.roads.project.service.IProReportService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 项目报Controller
 * 
 * @author nhp
 * @date 2022-06-08
 */
@RestController
@RequestMapping("/project/report")
public class ProReportController extends BaseController {

    @Autowired
    private IProReportService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProReport model) {
        startPage();
        List<ProReport> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "项目报表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProReport model) {
        List<ProReport> list = service.selectModelList(model);
        ExcelUtils<ProReport> util = new ExcelUtils<ProReport>(ProReport.class);
        return util.exportExcel(list, "项目报数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Integer reportId) {
        return AjaxResult.success(service.getModelById(reportId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "项目报表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProReport model) {
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProReport model) {
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "项目报表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        // 删除验证
        if (service.isQuote(ids)) {
            return error("报表已分配至用户或角色，不能进行删除");
        }
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 查询已分配角色报表列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/authReport/allocatedRoleReportList")
    public TableDataInfo allocatedRoleReportList(ProReport report) {
        startPage();
        List<ProReport> list = service.allocatedRoleReportList(report);
        return getDataTable(list);
    }

    /**
     * 查询已分配用户报表列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/authReport/allocatedUserReportList")
    public TableDataInfo allocatedUserReportList(ProReport report) {
        startPage();
        List<ProReport> list = service.allocatedUserReportList(report);
        return getDataTable(list);
    }

    /**
     * 查询未分配角色报表列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/authReport/unallocatedRoleReportList")
    public TableDataInfo unallocatedRoleReportList(ProReport report) {
        startPage();
        List<ProReport> list = service.unallocatedRoleReportList(report);
        return getDataTable(list);
    }

    /**
     * 查询未分配角色报表列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/authReport/unallocatedUserReportList")
    public TableDataInfo unallocatedUserReportList(ProReport report) {
        startPage();
        List<ProReport> list = service.unallocatedUserReportList(report);
        return getDataTable(list);
    }

    /**
     * 角色批量选择报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-角色授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authRoleReport")
    public AjaxResult authRoleReport(Long roleId, Integer[] reportIds) {
        return toAjax(service.authRoleReport(roleId, reportIds));
    }

    /**
     * 用户批量选择报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-用户授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authUserReport")
    public AjaxResult authUserReport(Long userId, Integer[] reportIds) {
        return toAjax(service.authUserReport(userId, reportIds));
    }

    /**
     * 取消角色报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-角色授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authRoleReportCancel")
    public AjaxResult authRoleReportCancel(@RequestBody SysRoleReport roleReport) {
        return toAjax(service.deleteAuthRoleReport(roleReport));
    }

    /**
     * 取消用户报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-用户授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authUserReportCancel")
    public AjaxResult authUserReportCancel(@RequestBody SysUserReport userReport) {
        return toAjax(service.deleteAuthUserReport(userReport));
    }

    /**
     * 批量取消角色报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-角色授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authRoleReportCancelAll")
    public AjaxResult authRoleReportCancelAll(Long roleId, Integer[] reportIds) {
        return toAjax(service.deleteAuthRoleReports(roleId, reportIds));
    }

    /**
     * 批量取消用户报表授权
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "项目报表-用户授权", businessType = BusinessType.GRANT)
    @PutMapping("/authReport/authUserReportCancelAll")
    public AjaxResult authUserReportCancelAll(Long userId, Integer[] reportIds) {
        return toAjax(service.deleteAuthUserReports(userId, reportIds));
    }

}
